﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Text;

namespace Card.Web
{
    public partial class Templates : BasePage
    {
        #region 属性
        /// <summary>
        /// 模版类型
        /// </summary>
        public string TemplateType
        {
            get
            {
                if (!string.IsNullOrEmpty(Request.QueryString["t"]))
                {
                    return Request.QueryString["t"];
                }
                return string.Empty;
            }
        }
        /// <summary>
        /// 颜色
        /// </summary>
        private string Color
        {
            get
            {
                if (!string.IsNullOrEmpty(Request.QueryString["c"]))
                {
                    return Request.QueryString["c"];
                }
                return string.Empty;
            }
        }
        /// <summary>
        /// 版式
        /// </summary>
        private string Style
        {
            get
            {
                if (!string.IsNullOrEmpty(Request.QueryString["s"]))
                {
                    return Request.QueryString["s"];
                }
                return string.Empty;
            }
        }
        /// <summary>
        /// 搜索关键字
        /// </summary>
        private string SearchWord
        {
            get
            {
                return Request.QueryString["w"] == null ? string.Empty : Request.QueryString["w"];
            }
        }
        #endregion

        #region
        protected void Page_Load(object sender, EventArgs e)
        {
            this.DoInit();
        }

        protected override void OnInit(EventArgs e)
        {
            this.pager.BuildingUrl += new BAF.WebControls.BuildingUrlHander(pager_BuildingUrl);
            base.OnInit(e);
        }

        string pager_BuildingUrl(int index)
        {
            
            return string.Format("templates.aspx?t={0}&c={1}&s={2}&i={3}{4}",this.TemplateType,this.Color,this.Style,index,string.IsNullOrEmpty(this.SearchWord)?string.Empty:string.Format("&w={0}",this.SearchWord));

        }
        #endregion

        #region
        private void DoInit()
        {
            List<BAF.Entity.LookupValue> lstColor = Common.LookUpCache.GetLookupValues(CONST.LOOKUP_TYPE.TEMPLATE_COLOR);
            Card.Utility.ControlUtility.BindListControl<BAF.Entity.LookupValue>(this.dropColor, lstColor, "Description", "LookupCode", false);
            this.dropColor.Items.Insert(0, new ListItem("全部颜色", ""));

            this.dropStyle.Items.AddRange(new ListItem[] { 
                new ListItem(){ Text="全部版式",Value=""},
                new ListItem(){ Text="横版",Value="h", Selected = (string.Compare("H",this.Style,true) == 0) },
                new ListItem(){ Text="竖版",Value="v", Selected = (string.Compare("V",this.Style,true) == 0)}
            });

            Card.Utility.ControlUtility.SelectItemByValue(this.dropColor, this.Color.ToUpper());


            
            this.rptList.DataSource = Common.LookUpCache.GetLookupValues(CONST.LOOKUP_TYPE.TEMPLATE_TYPE);
            this.rptList.DataBind();

            Card.DataBase.Pagination pagination = new Card.DataBase.Pagination()
            {
                TableName = "TEMPLATE",
                PageSize = this.pager.PageSize,
                OrderBy = "NAME,ID",
                PageNumber = this.pager.PageIndex + 1,
                Condition = this.GetQueryCondition()
            };

            DataTable dtData = BLL.TemplateManager.Query(pagination);
            this.pager.RecordCount = pagination.RecordCount;
            this.rptCardList.DataSource = dtData;
            this.rptCardList.DataBind();
        }

        public string GetQueryUrl()
        {
            return string.Empty;
        }

        private string GetQueryCondition()
        {
            StringBuilder sb = new StringBuilder("  AND STATUS='Y'");

            BAF.Entity.LookupValue lookUp = Common.LookUpCache.GetLookupValue(CONST.LOOKUP_TYPE.TEMPLATE_TYPE, this.TemplateType.ToUpper());
            if (lookUp != null)
            {                
                sb.Append(string.Format(" AND TYPE_CODE='{0}'", this.TemplateType.ToUpper()));
            }
            sb.Append(string.IsNullOrEmpty(this.SearchWord) ? string.Empty : string.Format(" AND (ID LIKE '%{0}%' OR NAME  LIKE '%{0}%' ) ", Card.Utility.TextUtility.JSUnescape(this.SearchWord)));
            return sb.ToString();
        }
        #endregion
    }
}
